package com.gitee.magic.framework.head.converter;

import java.util.Date;

import com.gitee.magic.core.converter.AbstractConverterEditor;
import com.gitee.magic.core.converter.DocDescription;

/**
 * 时间戳
 * @author start
 *
 */
@DocDescription("13位时间戳")
public class TimeStampConverterEditor extends AbstractConverterEditor<Long> {

	public TimeStampConverterEditor(Class<?> prototype) {
		super(prototype);
	}

	@Override
	public void restore(Object value) {
		if(value!=null) {
			setValue(restoreValue(value));
//			setValue(new Date(Long.parseLong(String.valueOf(value))));
		}
	}

	@Override
	public Long converter() {
		if(getValue()==null) {
			return null;
		}
		return ((Date)getValue()).getTime();
	}

	@Override
	public Object getSource() {
//		return String.valueOf(converter());
		return converter();
	}
	
	public static Object restoreValue(Object value) {
		Class<?> type=value.getClass();
		if(type.equals(String.class)||type.equals(Long.class)) {
			return new Date(Long.parseLong(String.valueOf(value)));
		}else {
			return com.gitee.magic.converter.DateConverterEditor.restoreDataValue(value);
		}
	}

}
